<?
//******************************************************************************
//******************               MEETME                  *********************
//******************************************************************************

/**
 * Get meetme conference room by id or get all if id is not defined. 
 * 
 * @param integer meetmeID
 * @return array meetme[]
 * @see getMeetmeByName
 * @see putMeetme
 * @see delMeetme
 * @todo make macro selectable and add more options how conference should work, also discuss how to handle macro and application because you should be able to rename or delete them if they are inuse I believe. Write frontend to manage a conference, implement a conference manager user type and converence recordings.
 */

function getMeetme($id) {

  global $db;

  if ($id != "") $condition = "WHERE id = '$id'";

  $qry="SELECT * FROM cw_meetme $condition ORDER BY confno";

  $res=$db->Execute($qry);

  while ($arr=$res->FetchRow()) { 
    $ret[]=$arr;
  }

  // Update pin and adminpin if somebody used editor to change them 

  if(is_array($ret)) {
    foreach ($ret as $meetme) {
      $var_val = $meetme["confno"];
      $qry="SELECT * FROM ast_config WHERE filename='meetme.conf' AND category='rooms' AND var_name = 'conf' AND var_val LIKE '$var_val%'";
      $res=$db->Execute($qry);

      $curMeetme=$res->FetchRow();
      $parMeetme=explode(",", $curMeetme["var_val"]);

      if (isset($parMeetme[1])) {
        $meetme["pin"]      = $parMeetme[1];
      }
      if (isset($parMeetme[2])) {
        $meetme["adminpin"] = $parMeetme[2];
      }

      // if meetme has already an application, attach it

      if ($meetme["app_id"]) {
        $meetme["application"] = getApplication($meetme["app_id"]);
      }
      
      $optionsSub=strstr($meetme["application"][0]["parameter"],"option=");
      if(strpos($optionsSub,"|")) $optionsSub=substr($optionsSub,0,strpos($optionsSub,"|"));
      $optionsSub=substr($optionsSub,strpos($optionsSub,"=")+1,(strlen($optionsSub) - strpos($optionsSub,"=")));
      $meetme["options"]=$optionsSub;
      
      $tmp[]=$meetme;
    } 
    $ret=$tmp;
  }
  return $ret;
}


/**
 * Get meetme conference room by name (confno)
 * 
 * @param string $name
 * @return array meetme[]
 * @see getMeetme
 * @see putMeetme
 * @see delMeetme
 * @todo make macro selectable and add more options how conference should work, also discuss how to handle macro and application because you should be able to rename or delete them if they are inuse I believe. Write frontend to manage a conference, implement a conference manager user type and converence recordings.
 */

function getMeetmeByName($name) {

  global $db;

  $qry="SELECT * FROM cw_meetme WHERE confno = '$name' ORDER BY confno";

  $res=$db->Execute($qry);

  while ($arr=$res->FetchRow()) { 
    $ret[]=$arr;
  }

  // Update pin and adminpin if somebody used editor to change them 

  if(is_array($ret)) {
    foreach ($ret as $meetme) {
      $var_val = $meetme["confno"];
      $qry="SELECT * FROM ast_config WHERE filename='meetme.conf' AND category='rooms' AND var_name = 'conf' AND var_val LIKE '$var_val%'";
      $res=$db->Execute($qry);

      $curMeetme=$res->FetchRow();
      $parMeetme=explode(",", $curMeetme["var_val"]);

      if (isset($parMeetme[1])) {
        $meetme["pin"]      = $parMeetme[1];
      }
      if (isset($parMeetme[2])) {
        $meetme["adminpin"] = $parMeetme[2];
      }

      // if meetme has already an application, attach it

      if ($meetme["app_id"]) {
        $meetme["application"] = getApplication($meetme["app_id"]);
      }
      
      $optionsSub=strstr($meetme["application"][0]["parameter"],"option=");
      if(strpos($optionsSub,"|")) $optionsSub=substr($optionsSub,0,strpos($optionsSub,"|"));
      $optionsSub=substr($optionsSub,strpos($optionsSub,"=")+1,(strlen($optionsSub) - strpos($optionsSub,"=")));
      $meetme["options"]=$optionsSub;
      
      $tmp[]=$meetme;
    } 
    $ret=$tmp;
  }
  return $ret;
}


/**
 * Create a new meetme conference room or recreate existing one
 * 
 * @param array $meetme
 * @param boolean $keepId default: true=keep Id regardless of the name; false=keep Id only if name matches
 * @return integer meetmeID
 * @see getMeetme
 * @see getMeetmeByName
 * @see delMeetme
 * @todo make macro selectable and add more options how conference should work, also discuss how to handle macro and application because you should be able to rename or delete them if they are inuse I believe. Write frontend to manage a conference, implement a conference manager user type and converence recordings.
 */

function putMeetme($meetme, $keepId=true) {

  global $db;

  // Request for Update? 
  // By name or by id?  
  // Update via edit would be by id
  // Update via plugin would be by confno
  // Otherwise we would risk to override a complete different entity via a plugin
  // So $keepId should be false if called via putPlugin.

  // validate data for meetme conference room

  if (($meetme["confno"]   == "") || !ctype_digit($meetme["confno"]))
    return 0;

  if (($meetme["pin"]      != "") && !ctype_digit($meetme["pin"]))
    return 0;

  if (($meetme["adminpin"] != "") && !ctype_digit($meetme["adminpin"]))
    return 0;

  // build room string = var_val for ast_config
  $var_val = $meetme["confno"];
  if ($meetme["pin"] != "") {
    $var_val.=",".$meetme["pin"];
    if ($meetme["adminpin"] != "") {
      $var_val.=",".$meetme["adminpin"];
    }
  } else { // Admin pin can only be used if we have a user pin
    $meetme["adminpin"] = "";
  }

  // lets check if the confno is already inuse

  if (!isset($meetme["id"])) {
    if(getMeetmeByName($meetme["confno"]))
      return 0;
  }

  // go to alterMeetme function

  if ((isset($meetme["id"])) and ($meetme["id"] != "") and ($keepId == true)) {
    // id is set for new meetme and must be used       
    $entity["id"] = $meetme["id"];
    delMeetme($entity["id"]);      
  } elseif ((isset($meetme["confno"])) and ($meetme["confno"] != "") and ($keepId == false)) {
    // confno is set for new meetme and must be used
    $curMeetme = getMeetmeByName($meetme["confno"]);
    if ($curMeetme[0]['confno'] == $meetme['confno']) {
      // confno matches, so get the id of the existing meetme
      $entity["id"] = $curMeetme[0]['id'];
      delMeetme($entity["id"]);      
    }
  }

  // create a new entity, use the existing id if this is an update

  $entity["type"] = MEETME;
  $entity["key"]  = 0;  // key is zero. The NUMBER entity hasn't a reference table, so no key has to be written
  $entity_id=putEntity($entity);

  if (is_object($entity_id) or $entity_id == 0) 
    return 0;

  // create application for this conference room
  $macroArr=getMacroByName("conference");
  $macro=$macroArr[0];
  $macro_id=$macro["id"];
  $macro_param="confno=".$meetme["confno"]."|option=".$meetme["options"];
  $app["name"]        = "conference-".$meetme['confno'];
  $app["label"]       = "Conference-".$meetme['confno'];
  $app["description"] = "Meetme Conference Room - ".$meetme['confno'];
  $app["extension"]   = $meetme['confno'];
  $app["macro"]       = $macro_id;
  $app["parameter"]   = $macro_param;
  $app["groups"]      = $meetme['application']['groups'];
  $app["id"]          = putApplication($app,false);
  // check if we have a new application
  if(!$app["id"]) {
    // if creating an application failed, delete the entity again
    delEntity($entity_id);
    return 0;
  }

  // insert meetme into cw_meetme table

  $qry = "INSERT INTO cw_meetme (`id`,`confno`,`pin`,`adminpin`,`app_id`,`app_name`) VALUES ('$entity_id','$meetme[confno]','$meetme[pin]','$meetme[adminpin]','$app[id]','$app[name]')";
  $res=$db->Execute($qry);
  if ($res===false) return new soap_fault('Server','','Meetme creation error, mysql INSERT command failed'); 

  // insert the meetme confno, pin, adminpin into ast_config table  

  $ret = putAstConfig("meetme.conf","rooms","conf",$var_val); 

  // return value

  if ($ret > 0) $ret = $entity_id;

  return $ret;
}


/**
 * Delete a meetme conference room by id
 * 
 * @param integer $meetmeID
 * @return integer 1 
 * @see getMeetme
 * @see putMeetme
 * @todo make macro selectable and add more options how conference should work, also discuss how to handle macro and application because you should be able to rename or delete them if they are inuse I believe. Write frontend to manage a conference, implement a conference manager user type and converence recordings.
 */

function delMeetme($id) {

  global $db;
  
  // retrieve the meetme conference info

  $meetme = getMeetme($id);

  // check if we have at least the conference number and app_id
  //
  // TODO:
  // I believe we should be using the application name in the future
  // However as long as its all local also the app_id should be
  // correct. Something need to be fixed in the future really.
  // I would fix the macro names of macros shipping with Vone....


//if ($meetme[0]["confno"] == "" || $meetme[0]["app_name"] == "") 
  if ($meetme[0]["confno"] == "" || $meetme[0]["app_id"] == 0) 
    return 0;

  // delete application for this conference room

  $app = getApplicationByName($meetme[0]["app_name"]);

  // problem here is, that one can change the application name
  // as well as macro names in the editor, I think this should
  // not be allowed for macros that ship with voiceone ...
  
  /**
   * Commented by Daniele: macro can be deleted in application section.
   * In that case, this check generate an error.  
   * Furthermore, application name is unique like id. 
   *
  if ($app[0]["id"] != $meetme[0]["app_id"]) { // Mismatch, what now ??
    error_log("Abort - mismatch, cannot delete meetme application");
    $info="Id:[".$meetme[0]["app_id"]."] Name:[".$meetme[0]["app_name"]."]";
    error_log($info);
    return 0;
  }
  */
  delApplication($meetme[0]["app_id"],true);

  // delete entry from the support table 

  $qry="DELETE FROM cw_meetme WHERE id = '$id'";
  $res=$db->Execute($qry);
  if ($res===false) return new soap_fault('Server','','Meetme remove error, mysql DELETE command failed');

  // remove the meetme related entry in the ast_config table

  $var_val = $meetme[0]["confno"];

  $qry="DELETE FROM ast_config WHERE filename = 'meetme.conf' AND category = 'rooms'AND var_val LIKE '$var_val%'";
  $res=$db->Execute($qry);
  if ($res===false) return new soap_fault('Server','','Meetme remove error, mysql DELETE command failed');

  return 1;
}

?>
